package com.dandelion.api.service;

import com.dandelion.api.dao.UserOrderDao;
import com.dandelion.api.domain.NewestOrder;
import com.dandelion.common.dao.auto.mapper.OrderItemMapper;
import com.dandelion.common.dao.auto.mapper.OrderMapper;
import com.dandelion.common.dao.auto.pojo.Order;
import com.dandelion.common.dao.auto.pojo.OrderCriteria;
import com.dandelion.common.dao.auto.pojo.OrderItem;
import com.dandelion.common.dao.auto.pojo.OrderItemCriteria;
import com.dandelion.common.domain.OrderStatusEnum;
import com.dandelion.common.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private UserOrderDao userOrderDao;

    public void insertNewOrder(Order order) {
        orderMapper.insertSelective(order);
    }

    public void insertOrderGoodsItem(OrderItem orderItem) {
        orderItemMapper.insertSelective(orderItem);
    }


    public List<Order> getOrderListProcessing(String mobile) {
        return getOrderList(mobile, OrderStatusEnum.PROCESSING);
    }

    public List<Order> getOrderListCancelled(String mobile) {
        return getOrderList(mobile, OrderStatusEnum.CANCELLED);
    }

    public List<Order> getOrderListFinished(String mobile) {
        return getOrderList(mobile, OrderStatusEnum.FINISHED);
    }

    private List<Order> getOrderList(String mobile, OrderStatusEnum orderStatus) {
        OrderCriteria criteria = new OrderCriteria();
        criteria
                .createCriteria()
                .andOrderCctMobileEqualTo(mobile)
                .andStatusEqualTo(orderStatus.getByteValue())
                .andIsDeletedEqualTo((byte) 0);

        return orderMapper.selectByExample(criteria);
    }

    public Order getOrderDetail(String orderNo) {
        OrderCriteria criteria = new OrderCriteria();
        criteria.createCriteria().andOrderNoEqualTo(orderNo).andIsDeletedEqualTo((byte) 0);
        List<Order> orderList = orderMapper.selectByExample(criteria);
        return CommonUtils.selectOne(orderList);
    }

    public List<OrderItem> getOrderGoodsItemList(String orderNo) {
        OrderItemCriteria criteria = new OrderItemCriteria();
        criteria.createCriteria().andOrderNoEqualTo(orderNo).andIsDeletedEqualTo((byte) 0);
        return orderItemMapper.selectByExample(criteria);
    }

    public List<Order> queryMyOrderProcessing(long userId) {
        return queryMyOrder(userId, OrderStatusEnum.PROCESSING);
    }

    public List<Order> queryMyOrderCancelled(long userId) {
        return queryMyOrder(userId, OrderStatusEnum.CANCELLED);
    }

    public List<Order> queryMyOrderFinished(long userId) {
        return queryMyOrder(userId, OrderStatusEnum.FINISHED);
    }

    public List<Order> queryMyOrder(long userId, OrderStatusEnum orderStatus) {
        OrderCriteria criteria = new OrderCriteria();
        criteria
                .createCriteria()
                .andUserIdEqualTo(userId)
                .andStatusEqualTo(orderStatus.getByteValue())
                .andIsDeletedEqualTo((byte) 0);

        return orderMapper.selectByExample(criteria);
    }

    public List<NewestOrder> queryNewestOrderList() {
        return userOrderDao.queryNewestOrder();
    }
}
